import ElementUI from "element-ui";

import nprogress from "nprogress";
// 拦截器  请求  响应
import axios from "axios";
const http = axios.create({
  withCredentials: true, //携带凭证
  baseURL: "/api",
  timeout: 5000, //超时处理
});

//请求 拦截
http.interceptors.request.use((config) => {
  nprogress.start();

  config.headers.authorization = localStorage.getItem("qf-token");
  return config;
});

// 响应 拦截
http.interceptors.response.use((config) => {
  nprogress.done();
  // config.="";
  let {
    data: { state, msg, code },
  } = config;
  //         过期登入 token过期  没有权限
  let codes = ["10022", "1004", "10026"];
  if (codes.includes(code)) {
    ElementUI.Message.error("登入过期，请重新登录！");
    localStorage.removeItem("qf-token");
    location.reload();
  }

  //   console.log(config);
  let iseExcludeUrl = ["/order/query_order_status"].includes(config.config.url);
  if (!state && !iseExcludeUrl) {
    ElementUI.MessageBox.alert(msg, "错误提示", {
      type: "error",
    });
  }
  return config;
});

export default http;
